package com.mbyte.easy.vacation.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mbyte.easy.vacation.entity.Vacation;
import com.mbyte.easy.vacation.service.IVacationService;
import com.mbyte.easy.common.controller.BaseController;
import com.mbyte.easy.common.web.AjaxResult;
import com.mbyte.easy.util.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.ObjectUtils;
import java.util.List;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
* <p>
* 前端控制器
* </p>
* @author lp
* @since 2019-03-11
*/
@Controller
@RequestMapping("/vacation/vacation")
public class VacationController extends BaseController  {

    private String prefix = "vacation/vacation/";

    @Autowired
    private IVacationService vacationService;

    /**
    * 查询列表
    *
    * @param model
    * @param pageNo
    * @param pageSize
    * @param vacation
    * @return
    */
    @RequestMapping
    public String index(Model model,@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize, String startTimeSpace, String endTimeSpace, String createTimeSpace, String deleteTimeSpace, Vacation vacation) {
        Page<Vacation> page = new Page<Vacation>(pageNo, pageSize);
        QueryWrapper<Vacation> queryWrapper = new QueryWrapper<Vacation>();
        if(!ObjectUtils.isEmpty(vacation.getUserId())) {
            queryWrapper = queryWrapper.like("user_id",vacation.getUserId());
         }
        if(!ObjectUtils.isEmpty(vacation.getReason())) {
            queryWrapper = queryWrapper.like("reason",vacation.getReason());
         }
        if(!ObjectUtils.isEmpty(vacation.getStartTime())) {
            queryWrapper = queryWrapper.like("start_time",vacation.getStartTime());
         }
        if(!ObjectUtils.isEmpty(vacation.getEndTime())) {
            queryWrapper = queryWrapper.like("end_time",vacation.getEndTime());
         }
        if(!ObjectUtils.isEmpty(vacation.getVacationStatus())) {
            queryWrapper = queryWrapper.like("vacationStatus",vacation.getVacationStatus());
         }
        if(!ObjectUtils.isEmpty(vacation.getVerifyStatus())) {
            queryWrapper = queryWrapper.like("verifyStatus",vacation.getVerifyStatus());
         }
        if(!ObjectUtils.isEmpty(vacation.getIsVerified())) {
            queryWrapper = queryWrapper.like("isVerified",vacation.getIsVerified());
         }
        if(!ObjectUtils.isEmpty(vacation.getDeleteTime())) {
            queryWrapper = queryWrapper.like("delete_time",vacation.getDeleteTime());
         }
        if(!ObjectUtils.isEmpty(vacation.getDeleteStatus())) {
            queryWrapper = queryWrapper.like("delete_status",vacation.getDeleteStatus());
         }
        IPage<Vacation> pageInfo = vacationService.page(page, queryWrapper);
        model.addAttribute("startTimeSpace", startTimeSpace);
        model.addAttribute("endTimeSpace", endTimeSpace);
        model.addAttribute("deleteTimeSpace", deleteTimeSpace);
        model.addAttribute("searchInfo", vacation);
        model.addAttribute("pageInfo", new PageInfo(pageInfo));
        return prefix+"list";
    }
    /**
    * 准假
    * @param id
    * @return
    */
    @GetMapping("permit/{id}")
    @ResponseBody
    public AjaxResult permit(@PathVariable("id") Long id){
        Vacation vacation = vacationService.getById(id);
        vacation.setIsVerified(1);
        vacation.setVerifyStatus(1);
        return toAjax(vacationService.updateById(vacation));
    }
    /**
    * 不准假
    * @param id
    * @return
    */
    @GetMapping("unPermit/{id}")
    @ResponseBody
    public AjaxResult unPermit(@PathVariable("id") Long id){
        Vacation vacation = vacationService.getById(id);
        vacation.setIsVerified(1);
        vacation.setVerifyStatus(0);
        return toAjax(vacationService.updateById(vacation));
    }
    /**
    * 批量准假
    * @param ids
    * @return
    */
    @PostMapping("permitAll")
    @ResponseBody
    public AjaxResult permitAll(@RequestBody List<Long> ids){
        for (int i = 0; i < ids.size(); i++) {
            Long id = ids.get(i);
            Vacation vacation = vacationService.getById(id);
            if(vacation.getIsVerified() == 1){
                continue;
            }
            vacation.setIsVerified(1);
            vacation.setVerifyStatus(1);
            vacationService.updateById(vacation);
        }
        return toAjax(1);
    }
    /**
    * 批量不准假
    * @param ids
    * @return
    */
    @PostMapping("unPermitAll")
    @ResponseBody
    public AjaxResult unPermitAll(@RequestBody List<Long> ids){
        for (int i = 0; i < ids.size(); i++) {
            Long id = ids.get(i);
            Vacation vacation = vacationService.getById(id);
            if(vacation.getIsVerified() == 1){
                continue;
            }
            vacation.setIsVerified(1);
            vacation.setVerifyStatus(0);
            vacationService.updateById(vacation);
        }
        return toAjax(1);
    }

}

